$(function () {
    $("#jqGrid").jqGrid({
        url: '../materialpublic/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '素材类型', name: 'type', index: 'type', width: 80 }, 			
			{ label: '素材细类型', name: 'category', index: 'category', width: 80 }, 			
			{ label: '素材版本', name: 'version', index: 'version', width: 80 }, 			
			{ label: 'SDK最小支持版本，在APP DEMO接口查询时需要', name: 'minAppVersion', index: 'min_app_version', width: 80 }, 			
			{ label: '素材包大小', name: 'packageSize', index: 'package_size', width: 80 }, 			
			{ label: '素材文件', name: 'packageRelativePath', index: 'package_relative_path', width: 80 }, 			
			{ label: '素材封面', name: 'coverRelativePath', index: 'cover_relative_path', width: 80 }, 			
			{ label: '某些素材添加后变化封面，在APP DEMO接口查询时需要,暂时只有推镜素材有', name: 'coverRelativePath2', index: 'cover_relative_path_2', width: 80 }, 			
			{ label: '素材预览视频', name: 'previewVideoRelativePath', index: 'preview_video_relative_path', width: 80 }, 			
			{ label: '特定素材(例如影集、mimo等)相关元数据信息URL，在APP DEMO接口查询时需要', name: 'infoRelativePath', index: 'info_relative_path', width: 80 }, 			
			{ label: '标题', name: 'displayName', index: 'display_name', width: 80 }, 			
			{ label: '中文标题', name: 'displayNameZhCn', index: 'display_name_zh_CN', width: 80 }, 			
			{ label: '用户自定义素材名称，用于在私有素材页中显示', name: 'customDisplayName', index: 'custom_display_name', width: 80 }, 			
			{ label: '素材简介', name: 'description', index: 'description', width: 80 }, 			
			{ label: '素材更新日志', name: 'updateLog', index: 'update_log', width: 80 }, 			
			{ label: '上传时间', name: 'initDate', index: 'init_date', width: 80 }, 			
			{ label: '跟新时间', name: 'modDate', index: 'mod_date', width: 80 }, 			
			{ label: '设计师ID', name: 'initUser', index: 'init_user', width: 80 }, 			
			{ label: '所属主用户ID', name: 'primaryUser', index: 'primary_user', width: 80 }, 			
			{ label: '素材是否通用标识', name: 'ratioFlag', index: 'ratio_flag', width: 80 }, 			
			{ label: '素材是否通用标识', name: 'supportedAspectRatio', index: 'supported_aspect_ratio', width: 80 }, 			
			{ label: '素材是否通用标识', name: 'defaultAspectRatio', index: 'default_aspect_ratio', width: 80 }, 			
			{ label: '模板总时长', name: 'templateTotalDuration', index: 'template_total_duration', width: 80 }, 			
			{ label: '模板片段数量', name: 'templateShotCount', index: 'template_shot_count', width: 80 }, 			
			{ label: '评级', name: 'quantity', index: 'quantity', width: 80 }, 			
			{ label: '-1删除 1正常', name: 'deleteState', index: 'delete_state', width: 80 }, 			
			{ label: ' 0私密  1公开', name: 'publicState', index: 'public_state', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		materialPublic: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.materialPublic = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.materialPublic.id == null ? "../materialpublic/save" : "../materialpublic/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.materialPublic),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../materialpublic/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../materialpublic/info/"+id, function(r){
                vm.materialPublic = r.materialPublic;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});